package com.L.yygh.order.service.impl;

import com.L.yygh.enums.RefundStatusEnum;
import com.L.yygh.model.order.PaymentInfo;
import com.L.yygh.model.order.RefundInfo;
import com.L.yygh.order.mapper.RefundInfoMapper;
import com.L.yygh.order.service.RefundService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RefundServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundService {
    /**
     * 保存退款的记录
     * @param paymentInfo
     * @return
     */
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        //判断是否有重复的数据添加
        QueryWrapper<RefundInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",paymentInfo.getOrderId());
        wrapper.eq("payment_type",paymentInfo.getPaymentType());
        RefundInfo refundInfo = baseMapper.selectOne(wrapper);
        if(refundInfo != null){ // 有相同数据
            return refundInfo;
        }
        //没有相同数据，添加记录
        RefundInfo refundInfoTemp = new RefundInfo();
        refundInfoTemp.setCreateTime(new Date());  //当前时间
        refundInfoTemp.setOrderId(paymentInfo.getOrderId());
        refundInfoTemp.setPaymentType(paymentInfo.getPaymentType());
        refundInfoTemp.setOutTradeNo(paymentInfo.getOutTradeNo());  //订单编号
        refundInfoTemp.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());  //退款的状态
        refundInfoTemp.setSubject(paymentInfo.getSubject());
        //paymentInfo.setSubject("test");
        refundInfoTemp.setTotalAmount(paymentInfo.getTotalAmount());   //订单金额
        baseMapper.insert(refundInfoTemp);
        return refundInfoTemp;
    }
}
